package com.hpin.fansea.ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.LoginResult;
import com.hpin.fansea.utils.CommonUtils;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;
import com.lidroid.xutils.http.RequestParams;

/**
 * 游客登录页面
 * Created by Aaron on 15/9/10.
 */
public class VisitorActivity extends BaseActivity {

    private LinearLayout ll_city;
    private LinearLayout ll_community;

    private TextView tv_city_select;
    private TextView tv_community_select;

    private Button btn_visitor_enter;

    private String cityId;
    private String communityId;
    private String cityName;
    private String communityName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mApplication.addActivity(this);

        setContentView(R.layout.activity_visitor);

        initView();
    }

    private void initView() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.vistor));

        ll_city = (LinearLayout) findViewById(R.id.ll_city);
        ll_community = (LinearLayout) findViewById(R.id.ll_community);

        tv_city_select = (TextView) findViewById(R.id.tv_city_select);
        tv_community_select = (TextView) findViewById(R.id.tv_community_select);

        btn_visitor_enter = (Button) findViewById(R.id.btn_visitor_enter);

        ll_city.setOnClickListener(this);
        ll_community.setOnClickListener(this);
        iv_left.setOnClickListener(this);
        btn_visitor_enter.setOnClickListener(this);

    }

    /**
     * 跳转城市列表选择页
     */
    private void citySelect() {
        Intent intent = new Intent(this, CityListActivty.class);
        startActivityForResult(intent, 0);
    }

    /**
     * 跳转小区列表选择页
     */
    private void communitySelect() {
        Intent intent = new Intent(this, CommunityListActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("cityId", cityId);
        intent.putExtras(bundle);
        startActivityForResult(intent, 1);
    }

//    /**
//     * 游客身份进入
//     */
//    private void visitorEnter() {
//        Intent intent = new Intent(this, HomeActivity.class);
//        startActivity(intent);
//        finish();
//    }

//    private Handler handler = new Handler() {
//
//        @Override
//        public void handleMessage(Message msg) {
//            ResultEntity ro = (ResultEntity) msg.obj;
//            if (ro == null) {
//                // 返回null
//                dismissProgress();
//                // showToast("服务器异常");
//                return;
//            }
//            switch (msg.what) {
//                case MessageDefine.LOGIN_VALIDATE:
//                    if (ro.getStatus().equals(Constant.SUCCESS)) {
//                        // 游客身份登陆成功
//                        ApplicationEx application = (ApplicationEx) VisitorActivity.this
//                                .getApplication();
//                        // 将信息保存到application
//                        application.userInfoByLogin = JSONObject.parseObject(
//                                ro.getUserInfo(), UserInfo.class);
//                        String token = ro.getToken();
//                        // 保存token
//                        Constant.token = token;
//                        // 保存小区
//                        Constant.COMMUNITY_ID = application.userInfoByLogin
//                                .getCommunityId();
//                        // 保存小区名称
//                        Constant.communityName = application.userInfoByLogin
//                                .getCommunityName();
//                        // 认证状态
//                        Constant.currentStatus = application.userInfoByLogin
//                                .getStatus();
//                        visitorEnter();
//                    } else {
//                        dismissProgress();
//                        showToast(ro.getMessage());
//                    }
//                    break;
//            }
//        }
//
//    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 10) {     //选择城市返回
            cityId = data.getStringExtra("cityId");
            cityName = data.getStringExtra("cityName");
            tv_city_select.setText(cityName);
        }
        if (resultCode == 20) {
            communityId = data.getStringExtra("communityId");
            communityName = data.getStringExtra("communityName");
            tv_community_select.setText(communityName);
        }

    }

    /**
     * 游客登录
     */
    public void vistorLogin() {

        if (CommonUtils.isNull(communityId)) {
            Toast.makeText(getApplicationContext(), "请选择小区!", Toast.LENGTH_SHORT).show();
            return;
        }


        login();

//        DataService.loginValidate(this, handler, "", "", communityId,
//                "1000500050002");

    }
        /**
         * 登录按钮事件
         */
    private void login() {

        RequestParams params=new RequestParams();
        params.addBodyParameter("lng", "");
        params.addBodyParameter("lat", "");
        params.addBodyParameter("systemType", Constant.SYSTEM_TYPE);// 客户端类型
        params.addBodyParameter("userId", "");//手机号码
        params.addBodyParameter("password", "");//密码
        params.addBodyParameter("communityId", communityId);//小区ID
        params.addBodyParameter("loginType", Constant.INTERFACE_USERTYPE_VISITOR);//登陆类型(游客)

        HttpUtil.getInstance().setPostRequest("登陆中...", this, Constant.WEB_ROOT + Constant.LOGIN, params, new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                try {

                    LoginResult loginResult = JSONObject.parseObject(str, LoginResult.class);

                    if (Constant.STATUS_SUCESS.equals(loginResult.status)) {
                        //登陆成功 保存用户数据
                        SharedPreferences.Editor edit = sp.edit();

                        edit.putString(Constant.token, loginResult.token);
                        edit.putString(Constant.communityId, loginResult.userInfo.communityId);
                        edit.putString(Constant.userName, loginResult.userInfo.userName);
                        edit.putString(Constant.userId, loginResult.userInfo.userId);
                        edit.putString(Constant.nickname, loginResult.userInfo.nickname);
                        edit.putString(Constant.headPic, loginResult.userInfo.headPic);
                        edit.putString(Constant.address, loginResult.userInfo.address);
                        edit.putString(Constant.password, loginResult.userInfo.password);
                        edit.putString(Constant.status, loginResult.userInfo.status);
                        edit.putString(Constant.email, loginResult.userInfo.email);
                        edit.putString(Constant.mobile, loginResult.userInfo.mobile);
                        edit.putString(Constant.cityId, loginResult.userInfo.cityId);
                        edit.putString(Constant.projectName, loginResult.userInfo.projectName);
                        edit.putString(Constant.houseCode, loginResult.userInfo.houseCode);
                        edit.putString(Constant.currentCommunityId, loginResult.userInfo.currentCommunityId);
                        edit.putString(Constant.houseId, loginResult.userInfo.houseId);
                        edit.putString(Constant.gender, loginResult.userInfo.gender);
                        edit.putString(Constant.birthday, loginResult.userInfo.birthday);
                        edit.putString(Constant.age, loginResult.userInfo.age);
                        edit.putString(Constant.idNo , loginResult.userInfo.idNo);
                        edit.putString(Constant.cityName, loginResult.userInfo.cityName);
                        edit.putString(Constant.communityName, loginResult.userInfo.communityName);
                        edit.putString(Constant.buildingNo, loginResult.userInfo.buildingNo);
                        edit.putString(Constant.unitNo, loginResult.userInfo.unitNo);
                        edit.putString(Constant.houseNo, loginResult.userInfo.houseNo);

                        edit.commit();

                        Intent intent = new Intent(VisitorActivity.this, HomeActivity.class);
                        startActivity(intent);
                            // finish();

                    }else {
                        showToast(loginResult.message);
                    }

                }catch (Exception e) {
                    e.printStackTrace();
                    showToast("登陆失败");
                }

            }
        }, null);

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {
            case R.id.iv_left:

                finish();
                break;

            case R.id.ll_city://选择城市

                citySelect();
                break;

            case R.id.ll_community://选择小区

                if (cityId == null) {
                    showToast("请先选择城市");
                } else {
                    communitySelect();
                }
                break;

            case R.id.btn_visitor_enter://游客登录

                vistorLogin();

                break;

            default:
                break;
        }
    }

}
